﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomUIControls;
using System.Data;
using System.Drawing;
using Lib.CommonForm;

namespace AUI
{
    public class LoadComboBox
    {

        public static void SetAutoDropDownWidth(object myCombo)
        {
            // auto-width of ComboBox's content
            int maxWidth = 0, temp = 0;
            if (myCombo is ComboBox)
            {
                ComboBox box = (ComboBox)myCombo;
                foreach (var obj in box.Items)
                {
                    temp = TextRenderer.MeasureText(box.GetItemText(obj), box.Font).Width;
                    if (temp > maxWidth)
                    {
                        maxWidth = temp;
                    }
                }
                box.DropDownWidth = maxWidth + SystemInformation.VerticalScrollBarWidth;
            }
            else if (myCombo is ToolStripComboBox)
            {
                ToolStripComboBox box = (ToolStripComboBox)myCombo;
                foreach (var obj in box.Items)
                {
                    temp = TextRenderer.MeasureText(box.ComboBox.GetItemText(obj), box.Font).Width;
                    if (temp > maxWidth)
                    {
                        maxWidth = temp;
                    }
                }
                box.DropDownWidth = maxWidth + SystemInformation.VerticalScrollBarWidth;
            }
        }

        public static void Years(object ctl)
        {

            int currenYear = DateTime.Today.Year;
            string YearWork = currenYear.ToString();// Properties.Settings.Default.YearWork.ToString();

            if (ctl is MultiColumnComboBox)
            {
                ComboBox box = (MultiColumnComboBox)ctl;
                box.Items.Add("");

                for (int i = currenYear - 5; i <= currenYear; i++)
                    box.Items.Add(i.ToString());
                box.Text = YearWork;
            }
            else if (ctl is ComboBox)
            {
                ComboBox box = (ComboBox)ctl;
                box.Items.Add("");

                for (int i = currenYear - 5; i <= currenYear; i++)
                    box.Items.Add(i.ToString());
                box.Text = YearWork;
            }
            else if (ctl is ToolStripComboBox)
            {
                ComboBox box = ((ToolStripComboBox)ctl).ComboBox;
                box.Items.Add("");

                for (int i = currenYear - 5; i <= currenYear; i++)
                    box.Items.Add(i.ToString());
                box.Text = YearWork;
            }
            else if (ctl is DataGridViewComboBoxColumn)
            {
                DataGridViewComboBoxColumn box = (DataGridViewComboBoxColumn)ctl;
                box.Items.Add("");

                for (int i = currenYear - 5; i <= currenYear; i++)
                    box.Items.Add(i.ToString());
            }

            //auto-width of combobox's content
            SetAutoDropDownWidth(ctl);
        }

        public static void Months(object ctl)
        {
            string PeriodWork = DateTime.Now.Month.ToString();//Properties.Settings.Default.PeriodWork.ToString();
            if (ctl is MultiColumnComboBox)
            {
                ComboBox box = (MultiColumnComboBox)ctl;
                box.Items.Add("");

                for (int i = 1; i < 13; i++)
                    box.Items.Add(i.ToString());
                box.Text = PeriodWork;
            }
            else if (ctl is ComboBox)
            {
                ComboBox box = (ComboBox)ctl;
                box.Items.Add("");

                for (int i = 1; i < 13; i++)
                    box.Items.Add(i.ToString());
                box.Text = PeriodWork;
            }
            else if (ctl is ToolStripComboBox)
            {
                ComboBox box = ((ToolStripComboBox)ctl).ComboBox;
                box.Items.Add("");

                for (int i = 1; i < 13; i++)
                    box.Items.Add(i.ToString());
                box.Text = PeriodWork;
            }
            else if (ctl is DataGridViewComboBoxColumn)
            {
                DataGridViewComboBoxColumn box = (DataGridViewComboBoxColumn)ctl;
                box.Items.Add("");

                for (int i = 1; i < 13; i++)
                    box.Items.Add(i.ToString());
            }

            //auto-width of combobox's content
            SetAutoDropDownWidth(ctl);
        }

        public static void Days(object ctl)
        {
            if (ctl is MultiColumnComboBox)
            {
                ComboBox box = (MultiColumnComboBox)ctl;
                box.Items.Add("");

                for (int i = 1; i < 32; i++)
                    box.Items.Add(i.ToString());
            }
            else if (ctl is ComboBox)
            {
                ComboBox box = (ComboBox)ctl;
                box.Items.Add("");

                for (int i = 1; i < 32; i++)
                    box.Items.Add(i.ToString());
            }
            else if (ctl is ToolStripComboBox)
            {
                ComboBox box = ((ToolStripComboBox)ctl).ComboBox;
                box.Items.Add("");

                for (int i = 1; i < 32; i++)
                    box.Items.Add(i.ToString());
            }
            else if (ctl is DataGridViewComboBoxColumn)
            {
                DataGridViewComboBoxColumn box = (DataGridViewComboBoxColumn)ctl;
                box.Items.Add("");

                for (int i = 1; i < 32; i++)
                    box.Items.Add(i.ToString());
            }

            //auto-width of combobox's content
            SetAutoDropDownWidth(ctl);
        }

        public static void Quarters(object ctl)
        {
            int quater = 1;
            int Month = DateTime.Today.Month;
            if (Month < 4)
                quater = 1;
            else if (Month < 7 && Month > 3)
                quater = 2;
            else if (Month < 10 && Month > 6)
                quater = 3;
            else if (Month > 9)
                quater = 4;


            if (ctl is MultiColumnComboBox)
            {
                ComboBox box = (MultiColumnComboBox)ctl;
                box.Items.Add("");

                for (int i = 1; i < 5; i++)
                    box.Items.Add(i.ToString());
                box.Text = quater.ToString();
            }
            else if (ctl is ComboBox)
            {
                ComboBox box = (ComboBox)ctl;
                box.Items.Add("");

                for (int i = 1; i < 5; i++)
                    box.Items.Add(i.ToString());
                box.Text = quater.ToString();
            }
            else if (ctl is ToolStripComboBox)
            {
                ComboBox box = ((ToolStripComboBox)ctl).ComboBox;
                box.Items.Add("");

                for (int i = 1; i < 5; i++)
                    box.Items.Add(i.ToString());
                box.Text = quater.ToString();
            }
            else if (ctl is DataGridViewComboBoxColumn)
            {
                DataGridViewComboBoxColumn box = (DataGridViewComboBoxColumn)ctl;
                box.Items.Add("");

                for (int i = 1; i < 5; i++)
                    box.Items.Add(i.ToString());
            }

            //auto-width of combobox's content
            SetAutoDropDownWidth(ctl);
        }


        #region Branch

        static void Branch(object ctl, string ValueMember, bool addFirstLine, string firtLineText)
        {

            DataTable dt = tbl_Branch_Controller.Instance().GetAll();

            string intValueMember = "BranchId";

            if (ctl is MultiColumnComboBox)
            {
                if (addFirstLine)
                {
                    DataRow dr = dt.NewRow();
                    dr[ValueMember] = (ValueMember.Equals(intValueMember)) ? "0" : "";
                    dr["BranchName"] = firtLineText;
                    dt.Rows.InsertAt(dr, 0);
                }

                MultiColumnComboBox box = (MultiColumnComboBox)ctl;
                box.DrawMode = DrawMode.OwnerDrawVariable;
                box.DropDownHeight = 400;
                box.FormattingEnabled = true;
                box.IntegralHeight = false;

                if (box.Width > 100)
                    box.DisplayMember = "BranchName";
                else
                    box.DisplayMember = "BranchCode";

                box.ValueMember = ValueMember;
                box.ColumnDisplays = new string[] { "BranchCode", "BranchName" };
                box.DataSource = dt;
                if (FormGlobals.isAdmin || FormGlobals.viewAllCompany)
                {
                    //MultiColumnComboBox Thường dùng để xem báo cáo hoặc nhập liệu, nên để mặc định là tất cả nếu có quyền quanTriHeThong hoặc xemDuLieuToanCongty

                    //if (Program.BranchID > 0)
                    //    box.SelectedValue = (ValueMember.Equals(intValueMember)) ? Program.BranchID.ToString() : Program.BranchCode;
                    //else
                    box.SelectedValue = (ValueMember.Equals(intValueMember)) ? "0" : "";
                    box.Enabled = true;
                }
                else
                {
                    box.SelectedValue = (ValueMember.Equals(intValueMember)) ? FormGlobals.BranchID.ToString() : FormGlobals.BranchCode;
                    box.Enabled = false;
                }
            }
            else
            {
                //Định nghĩa 1 bảng mới
                DataTable dtNew = dt.Clone();

                //Trích dữ liệu trích insert vào bảng mới tạo
                DataRow[] rowsToCopy = null;
                rowsToCopy = dt.Select("", "BranchCode ASC");
                foreach (DataRow item in rowsToCopy)
                {
                    dtNew.Rows.Add(
                            Globals.ToInt32(item["BranchId"]),
                            Globals.ToString(item["BranchCode"]),
                            Globals.ToString(item["BranchCode"]) + "    " + Globals.ToString(item["BranchShortName"]),
                            Globals.ToString(item["BranchCode"]) + "    " + Globals.ToString(item["BranchName"])
                        );
                }



                if (ctl is ComboBox)
                {
                    if (addFirstLine)
                    {
                        DataRow dr = dtNew.NewRow();
                        dr[ValueMember] = (ValueMember.Equals(intValueMember)) ? "0" : "";
                        dr["BranchName"] = firtLineText;
                        dtNew.Rows.InsertAt(dr, 0);
                    }

                    ComboBox box = (ComboBox)ctl;
                    box.DisplayMember = "BranchName";
                    box.ValueMember = ValueMember;
                    box.DataSource = dtNew;
                    //box.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if (FormGlobals.isAdmin || FormGlobals.viewAllCompany)
                    {
                        //ComboBox Thường dùng để xem báo cáo hoặc nhập liệu, nên để mặc định là tất cả nếu có quyền quanTriHeThong hoặc xemDuLieuToanCongty
                        //if (Program.BranchID > 0)
                        //    box.SelectedValue = (ValueMember.Equals(intValueMember)) ? Program.BranchID.ToString() : Program.BranchCode;
                        //else
                        box.SelectedValue = (ValueMember.Equals(intValueMember)) ? "0" : "";
                        box.Enabled = true;
                    }
                    else
                    {
                        box.SelectedValue = (ValueMember.Equals(intValueMember)) ? FormGlobals.BranchID.ToString() : FormGlobals.BranchCode;
                        box.Enabled = false;
                    }
                }
                else if (ctl is ToolStripComboBox)
                {
                    if (addFirstLine)
                    {
                        DataRow dr = dtNew.NewRow();
                        dr[ValueMember] = (ValueMember.Equals(intValueMember)) ? "0" : "";
                        dr["BranchName"] = firtLineText;
                        dtNew.Rows.InsertAt(dr, 0);
                    }

                    ComboBox box = ((ToolStripComboBox)ctl).ComboBox;
                    box.DropDownStyle = ComboBoxStyle.DropDownList;
                    box.DisplayMember = "BranchName";
                    box.ValueMember = ValueMember;
                    box.DataSource = dtNew;
                    //box.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if (FormGlobals.isAdmin || FormGlobals.viewAllCompany)
                    {
                        if (FormGlobals.BranchID > 0)
                            box.SelectedValue = (ValueMember.Equals(intValueMember)) ? FormGlobals.BranchID.ToString() : FormGlobals.BranchCode;
                        else
                            box.SelectedValue = (ValueMember.Equals(intValueMember)) ? "0" : "";
                        box.Enabled = true;
                    }
                    else
                    {
                        box.SelectedValue = (ValueMember.Equals(intValueMember)) ? FormGlobals.BranchID.ToString() : FormGlobals.BranchCode;
                        box.Enabled = false;
                    }
                }
                //auto-width of combobox's content
                SetAutoDropDownWidth(ctl);
            }

        }

        public static void BranchID_Input(object ctl)
        {
            Branch(ctl, "BranchId", true, "");
        }

        public static void BranchCode_Input(object ctl)
        {
            Branch(ctl, "BranchCode", true, "");
        }
        public static void BranchID_Filter(object ctl)
        {
            Branch(ctl, "BranchId", true, "--Tất cả--");
        }

        public static void BranchCode_Filter(object ctl)
        {
            Branch(ctl, "BranchCode", true, "--Tất cả--");
        }

        #endregion

        #region Department

        static void Department(object ctl, string ValueMember, object keyBranch, bool addFirstLine, string firstLineText)
        {
            DataTable dt = tbl_Department_Controller.Instance().GetAllByBranch(keyBranch);

            string intValueMember = "DepartmentId";

            if (ctl is MultiColumnComboBox)
            {
                if (addFirstLine)
                {
                    DataRow dr = dt.NewRow();
                    dr[ValueMember] = (ValueMember.Equals(intValueMember)) ? "0" : "";
                    dr["DepartmentName"] = firstLineText;
                    dt.Rows.InsertAt(dr, 0);
                }

                MultiColumnComboBox box = (MultiColumnComboBox)ctl;
                box.DrawMode = DrawMode.OwnerDrawVariable;
                box.DropDownHeight = 400;
                box.FormattingEnabled = true;
                box.IntegralHeight = false;

                if (box.Width > 100)
                    box.DisplayMember = "DepartmentName";
                else
                    box.DisplayMember = "DepartmentCode";

                box.ValueMember = ValueMember;
                box.ColumnDisplays = new string[] { "DepartmentCode", "DepartmentName" };
                box.DataSource = dt;

                if (FormGlobals.isAdmin || FormGlobals.viewAllCompany || FormGlobals.viewAllBranch)
                {
                    box.SelectedValue = (ValueMember.Equals(intValueMember)) ? "0" : "";
                    box.Enabled = true;
                }
                else
                {
                    box.SelectedValue = (ValueMember.Equals(intValueMember)) ? FormGlobals.DepartmentID.ToString() : FormGlobals.DepartmentCode;
                    box.Enabled = false;
                }
            }
            else
            {
                //Định nghĩa 1 bảng mới
                DataTable dtNew = dt.Clone();

                //Trích dữ liệu trích insert vào bảng mới tạo
                DataRow[] rowsToCopy = null;
                rowsToCopy = dt.Select("", "DepartmentCode ASC");
                foreach (DataRow item in rowsToCopy)
                {
                    dtNew.Rows.Add(
                            Globals.ToInt32(item["DepartmentId"]),
                            Globals.ToInt32(item["BranchID"]),
                            Globals.ToString(item["DepartmentCode"]),
                            Globals.ToString(item["DepartmentCode"]) + "    " + Globals.ToString(item["DepartmentName"])
                        );
                }


                if (ctl is ComboBox)
                {
                    if (addFirstLine)
                    {
                        DataRow dr = dtNew.NewRow();
                        dr[ValueMember] = (ValueMember.Equals(intValueMember)) ? "0" : "";
                        dr["DepartmentName"] = firstLineText;
                        dtNew.Rows.InsertAt(dr, 0);
                    }
                    ComboBox box = (ComboBox)ctl;

                    box.DisplayMember = "DepartmentName";
                    box.ValueMember = ValueMember;
                    box.DataSource = dtNew;
                    if (FormGlobals.isAdmin || FormGlobals.viewAllCompany || FormGlobals.viewAllBranch)
                    {
                        box.SelectedValue = (ValueMember.Equals(intValueMember)) ? "0" : "";
                        box.Enabled = true;
                    }
                    else
                    {
                        box.SelectedValue = (ValueMember.Equals(intValueMember)) ? FormGlobals.DepartmentID.ToString() : FormGlobals.DepartmentCode;
                        box.Enabled = false;
                    }
                }
                else if (ctl is ToolStripComboBox)
                {
                    if (addFirstLine)
                    {
                        DataRow dr = dtNew.NewRow();
                        dr[ValueMember] = (ValueMember.Equals(intValueMember)) ? "0" : "";
                        dr["DepartmentName"] = firstLineText;
                        dtNew.Rows.InsertAt(dr, 0);
                    }
                    ComboBox box = ((ToolStripComboBox)ctl).ComboBox;
                    box.DropDownStyle = ComboBoxStyle.DropDownList;
                    box.DisplayMember = "DepartmentName";
                    box.ValueMember = ValueMember;
                    box.DataSource = dtNew;
                    if (FormGlobals.isAdmin || FormGlobals.viewAllCompany || FormGlobals.viewAllBranch)
                    {
                        box.SelectedValue = (ValueMember.Equals(intValueMember)) ? "0" : "";
                        box.Enabled = true;
                    }
                    else
                    {
                        box.SelectedValue = (ValueMember.Equals(intValueMember)) ? FormGlobals.DepartmentID.ToString() : FormGlobals.DepartmentCode;
                        box.Enabled = false;
                    }
                }

                //auto-width of combobox's content
                SetAutoDropDownWidth(ctl);
            }
        }

        public static void DepartmentID_Input(object ctl, object keyBranch)
        {
            Department(ctl, "DepartmentId", keyBranch, true, "");
        }

        public static void DepartmentCode_Input(object ctl, object keyBranch)
        {
            Department(ctl, "DepartmentCode", keyBranch, true, "");
        }
        public static void DepartmentID_Filter(object ctl, object keyBranch)
        {
            Department(ctl, "DepartmentId", keyBranch, true, "--Tất cả--");
        }

        public static void DepartmentCode_Filter(object ctl, object keyBranch)
        {
            Department(ctl, "DepartmentCode", keyBranch, true, "--Tất cả--");
        }
        #endregion
    }
}
